<?php

namespace app\admin\controller\general;

use app\common\controller\Backend;
use app\common\library\Email;
use app\common\model\Config as ConfigModel;
use think\Cache;
use think\Db;
use think\Exception;
use think\Validate;

/**
 * 系统积分设置
 *
 * @icon   fa fa-cogs
 * @remark 可以在此增改系统的变量和分组,也可以自定义分组和变量,如果需要删除请从数据库中删除
 */
class ScoreConfig extends Backend
{

    /**
     * @var \app\common\model\Config
     */
    protected $model = null;
    protected $noNeedRight = ['check', 'rulelist', 'selectpage', 'get_fields_list'];

    public function _initialize()
    {
        parent::_initialize();
        // $this->model = model('Config');
        $this->model = new ConfigModel;
        ConfigModel::event('before_write', function ($row) {
            if (isset($row['name']) && $row['name'] == 'name' && preg_match("/fast" . "admin/i", $row['value'])) {
                throw new Exception(__("Site name incorrect"));
            }
        });
    }

    /**
     * 编辑
     * @param null $ids
     */
    public function index($ids = null)
    {
        $row = ConfigModel::where('group','score_config')->select();
        $name_arr = [];
        foreach($row as $v){
            $name_arr[] = $v['name'];
        }
        $this->view->assign("row",$row);

        if ($this->request->isPost() == false) {
            return $this->view->fetch('general/score_config/edit');
        } else {
            $row = $this->request->param();
            foreach($row as $k=>$v){
                if (in_array($k,$name_arr)){
                    ConfigModel::where('name',$k)->update(['value'=>$v]);
                }
            }
        }
        $this->success();
    }

}
